package Test;

import com.MonkWorks.clockDTO.User;
import com.MonkWorks.timedormitoryDB.IUserDB;
import com.MonkWorks.timedormitoryDB.UserDBStub;



import junit.framework.TestCase;

public class UserTest extends TestCase{

	IUserDB userDB;
	
	@Override
	protected void setUp() throws Exception{
		super.setUp();
		userDB = new UserDBStub();
	}
	/**
	 * Test fetch user
	 * Pass if all attributes match
	 */
	public void testFetch(){
		try {
			User testuser = userDB.fetchUser("TestUser");
			assertEquals(testuser.getFirstName(), "User");
			assertEquals(testuser.getLastName(), "Test");
			assertEquals(testuser.getemail(), "test@email.com");
			assertEquals(testuser.getPhoneNumber(), "1234567891");
			assertEquals(testuser.getUserType(), "admin");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * Test the save method
	 * Pass if method is able to process normally and throw an exception if incorrect info is passed
	 */
	public void testSave(){
		User validUser = new User();
		validUser.setUsername("Ckadmin");
		validUser.setPassword("123456");
		validUser.setFirstName("Chavez");
		validUser.setLastName("Kattick");
		validUser.setEmail("kattick@mail.com");
		validUser.setUserType("Admin");
		try{
			userDB.saveUser(validUser);
		}
		catch(Exception e)
		{
			fail("Should have saved this user");
		}
		//test bad user
		User baduser = new User();
		try{
			userDB.saveUser(baduser);
			fail("Should throw an error");
		}
		catch(Exception e)
		{
			
		}
	}
	/**
	 * Test fetchAll
	 * Pass if all values based on user match
	 */
	public void testFetchAll() {
		java.util.HashMap<String, User> allUsers = userDB.fetchAll();
		User testUser = allUsers.get("User");
		assertNotNull(testUser);
		assertEquals(testUser.getUsername(), "User");
		assertEquals(testUser.getPassword(), "User");
		assertEquals(testUser.getFirstName(), "Test");
		assertEquals(testUser.getLastName(), "Tester");
		assertEquals(testUser.getemail(), "test@email.com");
		assertEquals(testUser.getUserType(), "Normal");
	}
	
	/**test Login
	 * Pass if able to login
	 */
	public void testLogon(){
		String username = "Admin";
		String password = "Password";
		try
		{
			userDB.Logon(username, password);
		}
		catch (Exception e)
		{
			fail("fail logon");
		}
	}

}

